package com.mw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mw.anno.RateLimiter;
import com.mw.commons.utils.StringUtils;
import com.mw.pojo.SysLoginUser;
import com.mw.resultex.ex.OrderException;
import com.mw.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: IndexController
 * 类文件简单描述:
 * @Author: MW
 * @CreateDate: 2022/2/4 11:51
 */
@RestController
public class TestController {

    @Autowired
    private IUserService userService;

    @GetMapping("/indexTest")
    @RateLimiter(timeout = 1, limit = 3)
    public String indexTest() {
        if (true) {
            throw new OrderException(600, "测试订单有误");
        }
        return "Hello World";
    }

    @PostMapping("/saveUser")
    public SysLoginUser saveUser(@RequestBody SysLoginUser user) {
        boolean save = userService.save(user);
        return save ? user : null;
    }

    @PostMapping("/getLoginUser")
    public List<SysLoginUser> getLoginUser(SysLoginUser user) {
        QueryWrapper<SysLoginUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(user.getUserType()), "user_type", user.getUserType()).eq(!StringUtils.isEmpty(user.getStatus()), "status", user.getStatus());
        return userService.list(queryWrapper);
    }

    @GetMapping("/selectAll")
    public List<SysLoginUser> findUser(int pageNum, int pageSize) {
        Page<SysLoginUser> page = new Page<>(pageNum, pageSize);
        List<SysLoginUser> records = userService.page(page, null).getRecords();
        return records;
    }

    @GetMapping("/selectPage")
    public Page<SysLoginUser> findUserPage(int pageNum, int pageSize) {
        Page<SysLoginUser> page = new Page<>(pageNum, pageSize);
        Page<SysLoginUser> sysLoginUserPage = userService.page(page, null);
        return sysLoginUserPage;
    }

}